"""应用配置加载模块."""

from functools import lru_cache
from typing import Optional

from pydantic import Field

try:
    from pydantic_settings import BaseSettings
except ImportError:  # pragma: no cover - 兼容旧版 pydantic
    from pydantic import BaseSettings  # type: ignore


class Settings(BaseSettings):
    """全局配置."""

    openai_api_key: Optional[str] = Field(
        default=None, description="OpenAI API 密钥，来自环境变量或 .env 文件。"
    )
    api_base_url: Optional[str] = Field(
        default=None,
        description="OpenAI API 的自定义基础 URL，可用于代理或自建服务。",
    )
    default_model: str = Field(
        default="gpt-4o-mini",
        description="默认使用的大模型名称，用于 LangChain LLM 初始化。",
    )
    environment: str = Field(default="dev", description="运行环境标签。")

    class Config:
        env_file = ".env"
        env_prefix = "QAA_"
        case_sensitive = False


@lru_cache(maxsize=1)
def get_settings() -> Settings:
    """返回缓存的设置对象."""

    return Settings()

